package com.liziyi0914.morearmor.effect;

import com.liziyi0914.morearmor.PlayerManager;
import com.liziyi0914.morearmor.Utils;
import com.liziyi0914.morearmor.event.Listener;
import com.liziyi0914.morearmor.event.entity.WorldTickEvent;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.SmeltingRecipe;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import java.util.Optional;
import java.util.function.Predicate;

public class FurnaceEffect extends AbstractEffect {

    @Listener
    public void tick(WorldTickEvent e) {
        for (Entity entity : e.getWorld().getEntities()) {
            if (entity instanceof LivingEntity livingEntity) {
                if (!predicateArmor().test(livingEntity)) {
                    continue;
                }
                Vec3d p = livingEntity.getPos();
                final ServerPlayerEntity playerEntity = PlayerManager.get(entity.getUuid());
                if (playerEntity == null) continue;
                World world=playerEntity.getServerWorld();
                world.getEntitiesByType(EntityType.ITEM, new Box(p.getX() - 5, p.getY() - 5, p.getZ() - 5, p.getX() + 5, p.getY() + 5, p.getZ() + 5), en -> entity.getPos().distanceTo(en.getPos()) < 5)
                        .forEach(itemEntity -> {
                            if(itemEntity.cannotPickup()){
                                return;
                            }
                            ItemStack stack = itemEntity.getStack();
                            Optional<SmeltingRecipe> optional = world.getRecipeManager().getFirstMatch(RecipeType.SMELTING, new SimpleInventory(stack), world);
                            if (optional.isPresent()) {
                                ItemStack itemStack = optional.get().getOutput();
                                if (!itemStack.isEmpty()) {
                                    ItemStack itemStack2 = itemStack.copy();
                                    itemStack2.setCount(stack.getCount());
                                    final ItemEntity ie = new ItemEntity(world, itemEntity.getX(), itemEntity.getY(), itemEntity.getZ(), itemStack2);
                                    ie.setPickupDelay(40);
                                    ie.setThrower(playerEntity.getUuid());
                                    world.spawnEntity(ie);
                                    itemEntity.kill();
                                }
                            }
                        });
            }
        }
    }

    @Override
    public Predicate<LivingEntity> predicateArmor() {
        return Utils.hasItemArmor(Items.FURNACE);
    }

}
